package com.qikecn.update;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 解析软件更新信息
 * @author wangst
 *
 */
public class VersionObject {
	/**
	 * 解析为对象
	 * @param jsonData
	 * @return
	 * @throws JSONException
	 */
	public static VersionBean parseObjFromJson(String jsonData) throws JSONException{
		VersionBean bean = new VersionBean();
		JSONObject obj = new JSONObject(jsonData);
		bean.setVersionCode(obj.has("versionCode")?obj.getInt("versionCode"):-1);
		bean.setVersionMsg(obj.has("versionMsg") ? obj.getString("versionMsg") : "");
		bean.setDownloadUrl(obj.has("downloadUrl") ? obj.getString("downloadUrl") : "");
		bean.setForceUpVersionCode(obj.has("forceUpVersionCode") ? obj.getInt("forceUpVersionCode") : -1);
		bean.setFileSize(obj.has("fileSize") ? obj.getInt("fileSize") : -1);
		return bean;
	}

	public static VersionBean parseObjFromXml(String xmlData) throws Exception{
		xmlData = xmlData.replace("\\n","\n");
		VersionBean bean = new VersionBean();
		// 由android.util.Xml创建一个XmlPullParser实例
        /*XmlPullParser parser = Xml.newPullParser();
        StringReader reader = new StringReader(xmlData);
        parser.setInput(reader);
        // 产生第一个事件
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
            // 判断当前事件是否为文档开始事件
            case XmlPullParser.START_DOCUMENT:
                break;
            // 判断当前事件是否为标签元素开始事件
            case XmlPullParser.START_TAG:
                if (parser.getName().equals("versionCode")) {
                	bean.setVersionCode(Integer.parseInt(parser.nextText()));
                } else if (parser.getName().equals("versionMsg")) {
                	bean.setVersionMsg(parser.nextText());
                } else if (parser.getName().equals("downloadUrl")) {
                	bean.setDownloadUrl(parser.nextText());
                } else if (parser.getName().equals("forceUp")) {
                	bean.setForceUp(Boolean.parseBoolean(parser.nextText()));
                }
                break;
            // 判断当前事件是否为标签元素结束事件
            case XmlPullParser.END_TAG:
                break;
            }
            // 进入下一个元素并触发相应事件
            eventType = parser.nextTag();
        }
        reader.close();*/
		int versionCode = Integer.parseInt(xmlData.substring(xmlData.indexOf("<versionCode>") + 13, xmlData.indexOf("</versionCode>")));
		System.out.println("versionCode=" + versionCode);
		bean.setVersionCode(versionCode);
		String versionMsg = xmlData.substring(xmlData.indexOf("<versionMsg>")+12, xmlData.indexOf("</versionMsg>"));
		System.out.println("versionMsg=" + versionMsg);
		bean.setVersionMsg(versionMsg);
		String downloadUrl = xmlData.substring(xmlData.indexOf("<downloadUrl>") + 13, xmlData.indexOf("</downloadUrl>"));
		System.out.println("downloadUrl=" + downloadUrl);
		bean.setDownloadUrl(downloadUrl);
		if (xmlData.contains("forceUpVersionCode")){
			int forceUpVersionCode = Integer.parseInt(xmlData.substring(xmlData.indexOf("<forceUpVersionCode>") + 20, xmlData.indexOf("</forceUpVersionCode>")));
			System.out.println("forceUpVersionCode=" + forceUpVersionCode);
			bean.setForceUpVersionCode(forceUpVersionCode);
		}
		if (xmlData.contains("fileSize")){
			int fileSize = Integer.parseInt(xmlData.substring(xmlData.indexOf("<fileSize>") + 10, xmlData.indexOf("</fileSize>")));
			System.out.println("fileSize=" + fileSize);
			bean.setFileSize(fileSize);
		}
		return bean;
	}
}